package com.itboye.ihomebank.activity.finance;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.MyWalletBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.NetPublicConstant;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.presenter.JinRongPresenter;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;
import com.itboye.ihomebank.util.WritePassPopupWindow;
import com.itboye.ihomebank.web.WebActivity;

import java.util.Observable;
import java.util.Observer;

/**
 * Created by itboye on 2017/2/20.
 */

public class PurchaseActivity extends BaseOtherActivity implements Observer {

    private EditText editttextPurchanse;//购买金额
    private String Purchanse;
    private TextView add_shap_title_tv;
    TextView tvtongyi;
    private LinearLayout lineGoumai;
    private ImageView close_icon;
    private JinRongPresenter presenter;
    private String title;
    private String bili;
    private String shengyu;
    private String qixian;
    private String qitou;
    String id;
    private TextView tvPurchseTitle;//标题
    private TextView tvPurchsebaifenbi;//期望率
    private TextView tvPurchseqixian;//期限
    private TextView tvPurchseshengyue;//剩余
    private TextView tvPrice;
    private String uid;
    private UserPresenter userPresenter;
    TextView tv_qitou;

    private WritePassPopupWindow passPopupWindow;
    EditText pass;

    @Override
    protected int layout_id() {
        return R.layout.activity_purchase;
    }

    @Override
    protected void onInitController() {

    }

    @Override
    protected void onInit() {
        oniniData();
        add_shap_title_tv.setText("购买");
        title = getIntent().getStringExtra("title");
        id = getIntent().getStringExtra("id");
        bili = getIntent().getStringExtra("bili");
        shengyu = getIntent().getStringExtra("shengyu");
        qixian = getIntent().getStringExtra("qixian");
        qitou = getIntent().getStringExtra("qitou");
        tv_qitou.setText(qitou + "元起投,且以0元整数倍递增");
        tvPurchseTitle.setText(title);
        tvPurchsebaifenbi.setText(bili);
        tvPurchseqixian.setText(qixian);
        tvPurchseshengyue.setText(shengyu);
        presenter = new JinRongPresenter(this);
        userPresenter = new UserPresenter(this);
        uid = (String) SPUtils.get(this, null, SPContants.USER_ID, "");
        userPresenter.myWallet(uid);
        showProgressDialog("数据加载中...", true);
    }


    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.lineGoumai:
                passPopupWindow = new WritePassPopupWindow(this, this);
                pass = (EditText) passPopupWindow.getContentView()
                        .findViewById(R.id.pass);
                passPopupWindow.setBackgroundDrawable(new BitmapDrawable());
                passPopupWindow.setFocusable(true);
                passPopupWindow.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
                passPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
                passPopupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);



                break;
            case R.id.close_icon:
                this.finish();
                break;

            case R.id.weituoshu:
                Intent intent = new Intent(this, WebActivity.class);
                intent.putExtra(
                        "url",
                        NetPublicConstant.WEB_URL
                                + "/p2p/contract?id="
                                + id + "");
                intent.putExtra("title", "授权委托书");
                startActivity(intent);
                break;
            case R.id.btn_ok:
                String password = pass.getText().toString();
                if (password.equals("")) {
                    ByAlert.alert("请输入余额支付密码");
                    return;
                }
                userPresenter.yueYanzheng(uid, password);
                passPopupWindow.dismiss();
                break;
            case R.id.btn_cancel:
                passPopupWindow.dismiss();
                break;
        }
    }

    public void oniniData() {
        lineGoumai.setClickable(false);
        add_shap_title_tv.setText("购买");
        Purchanse = editttextPurchanse.getText().toString();

        editttextPurchanse.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (editttextPurchanse.getText().toString().length() > 0) {
                    lineGoumai.setClickable(true);//设置可点击
                    lineGoumai.setBackgroundResource(R.drawable.linlayout_shapre);

                } else {
                    lineGoumai.setClickable(false);//设置不可点击
                    lineGoumai.setBackgroundResource(R.drawable.linear_grare);

                }
            }

            @Override
            public void afterTextChanged(Editable s) {


            }
        });

    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);

        if (entity != null) {
            if (!entity.getCode().equals(0)) {
                if (entity.getEventType() == JinRongPresenter.tou_zi_goumai_success) {

                    ByAlert.alert(entity.getData() + "");
                    Intent intent=new Intent(this,GouManiOkActivity.class);
                    startActivity(intent);

                    finish();

                    Intent intent1 = new Intent("goumaiend");
                    sendBroadcast(intent1);
                }
                if (entity.getEventType() == JinRongPresenter.tou_zi_goumai_fail) {

                    ByAlert.alert(entity.getData() + "");
                }
            }

            if (entity.getEventType() == UserPresenter.yzyue_success) {
                showProgressDialog("购买中...", true);
                Purchanse = editttextPurchanse.getText().toString();
                Double d = Double.parseDouble(Purchanse);
                double db = d * 100;
                presenter.myGoumai(uid, id, db + "");
            }
            if (entity.getEventType() == UserPresenter.yzyue_fail) {
                ByAlert.alert(entity.getData());
            }
            if (entity.getEventType() == UserPresenter.wallet_success) {
                MyWalletBean myWalletBean = (MyWalletBean) entity.getData();
                tvPrice.setText("可用余额" + Double.parseDouble(myWalletBean
                        .getBalance() + "") / 100 + "元");
                try {
                    closeProgressDialog();
                } catch (Exception e) {
                }

            } else if (entity.getEventType() == UserPresenter.wallet_success) {
                ByAlert.alert(entity.getData().toString());
            }
        }
        try {
            closeProgressDialog();
        } catch (Exception e) {
        }
    }
}
